package com.team2.outpatient_sys.dao;

import com.team2.outpatient_sys.pojo.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Result;

import java.util.List;

@Mapper
public interface RegisteredDao {
    //新建挂号信息
    @Insert("insert into registered values(null,#{patientId},#{doctorId},#{userId},#{deptId},#{serialNumber},#{status}, #{timeQuantum},#{regTime},#{clinicTime})")
    public int add(Registered registered);
    //更改挂号状态
    @Update("update registered set status=#{status} where id=#{id}")
    public int update(Registered registered);
    //查询所有挂号信息
    @Select("select * from registered order by id desc")
    public List<Registered> findAll();
    //删除挂号信息
    @Delete("delete from registered where id=#{id}")
    public int delete(int id);
    //根据id查询挂号详情
    @Select("select * from registered where id=#{id}")
    public Registered findById(int id);
    //根据病人id查询挂号信息
    @Select("select * from registered where patient_id=#{patientId}")
    public List<Registered> findByPatientId(int patientId);
    //根据医生id查询挂号信息
    @Select("select * from registered where doctor_id=#{doctorId}")
    public List<Registered> findByDoctorId(int doctorId);
    //根据科室查询挂号信息
    @Select("select * from registered where dept_id=#{deptId}")
    public List<Registered> findByDeptId(int deptId);
    //根据时间查询挂号信息
    @Select("select * from registered where clinic_time=#{clinicTime}")
    public List<Registered> findByClinicTime(String clinicTime);
    //根据用户id查询查询挂号信息
    @Select("select * from registered where user_id=#{userId}")
    public Registered findByUserId(Long userId);
    //修改病人挂号状态
    @Update("update registered set status=#{status} where time_quantum=#{timeQuantum} and patient_id=#{patientId}")
    public void updateByPatentTime(Registered registered);
    //查询待签到病人
    @Select("select * from registered where status=1 and clinic_time=#{clinicTime} order by id desc")
    public List<Registered> findSign(String clinicTime);
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "serialNumber", column = "serial_number"),
            @Result(property = "status", column = "status"),
            @Result(property = "timeQuantum", column = "time_quantum"),
            @Result(property = "regTime", column = "reg_time"),
            @Result(property = "clinicTime", column = "clinic_time"),
            @Result(property = "patient", column = "patient_id",javaType = Patient.class,one = @One(select = "com.team2.outpatient_sys.dao.PatientDao.findById")),
            @Result(property = "doctor", column = "doctor_id",javaType = Doctor.class,one = @One(select = "com.team2.outpatient_sys.dao.DoctorDao.queryById")),
            @Result(property = "dept", column = "dept_id",javaType = Department.class,one = @One(select = "com.team2.outpatient_sys.dao.DepartmentDao.queryById"))
    })
    //查看挂号详细信息
    @Select("select * from registered order by id desc")
    public List<RegisteredDetailed> findRegisteredDetailed();
    //检查过期的挂号
    @Update("update registered set status=0 where (status=1 or status=2) and clinic_time<#{clinicTime}")
    public int overdue(String clinicTime);
    //查看当天排班医生
    @Select("SELECT DISTINCT d.* FROM SCHEDULE s,doctor_schedule ds,doctor d WHERE s.week=#{week} AND s.dept_id=#{deptId} AND s.id=ds.schedule_id AND s.dept_id=d.dept_id AND ds.doctor_id=d.id")
    public List<Doctor> findDoctor(Integer week,Integer deptId);
    //查看医生班期
    @Select("SELECT ds.schedule_id,s.shift,ds.appointment_no FROM doctor_schedule ds,SCHEDULE s WHERE s.week=#{week} AND s.dept_id=#{deptId} AND s.id=ds.schedule_id AND ds.doctor_id=#{doctorId}")
    public List<DoctorShift> findDoctorShift(Integer week,Integer deptId,Integer doctorId);
    //挂号成功剩余数减1
    @Update("update doctor_schedule set appointment_no=appointment_no-1 where doctor_id=#{doctorId} and schedule_id=#{scheduleId}")
    public int registeredSuccess(Integer doctorId,Integer scheduleId);
}
